// bbbbbbb

#include <common/sys_config.h>
#include <components/log.h>
#include <os/mem.h>
#include <driver/uart.h>
#include <driver/gpio.h>
#include "gpio_driver.h"

#include "time/ntp.h"
#include "time/time.h"

#include <driver/aon_rtc.h>
#include "aon_rtc_hal.h"
#include "aon_rtc_driver.h"
// #include "doorbell_at.h"

#include "components/bluetooth/bk_ble.h"
#include "components/bluetooth/bk_dm_ble.h"
#include "components/bluetooth/bk_dm_bluetooth.h"
#include "sys_driver.h"

#include <common/bk_include.h>
#include "uart_driver.h"
#include <driver/uart.h>
#include "bk_uart.h"
#include <os/mem.h>
#include <common/sys_config.h>
#include <components/log.h>
// #include "doorbell_at.h"
#include "dev_log.h"
#include "bkwl_uart.h"

#define DOORBELL_UART2_ID UART_ID_2
#define DOORBELL_UART2_BUF_LEN 128
#define DOORBELL_UART2_RX_TIMEOUT BEKEN_WAIT_FOREVER

#define DOORBELL_TASK_PRIO BEKEN_DEFAULT_WORKER_PRIORITY
#define DOORBELL_TASK_STACK_SIZE 1024

beken_thread_t doorbell_uart2_rec_thread_handle = NULL;
beken_thread_t doorbell_uart2_send_thread_handle = NULL;

void doorbell_uart2_receiver_thread(void);
void doorbell_uart2_send_thread(void);

void doorbell_uart2_callback1(uart_id_t id, void *param)
{
    tprintf("uart2 callback..................\r\n");
}

int doorbell_uart2_init(void)
{
    int ret;
    tprintf("doorbell_uart2_init  entry\n");

#if 0
    const uart_config_t config =
        {
            .baud_rate = 9600,
            .data_bits = UART_DATA_8_BITS,
            .parity = UART_PARITY_NONE,
            .stop_bits = UART_STOP_BITS_1,
            .flow_ctrl = UART_FLOWCTRL_DISABLE,
            .src_clk = UART_SCLK_XTAL_26M

        };
 
    tprintf("doorbell_uart2_init  0\n");
    ret = bk_uart_init(DOORBELL_UART2_ID, &config);
    if (ret != BK_OK)
    {
        bk_uart_deinit(DOORBELL_UART2_ID);
        tprintf("uart2 INIT ERROR\r\n");
        return 1;
    }
    tprintf("doorbell_uart2_init  0 0\n");
    bk_uart_enable_rx_interrupt(DOORBELL_UART2_ID);
    tprintf("doorbell_uart2_init  0 1\n");
    bk_uart_register_rx_isr(DOORBELL_UART2_ID, doorbell_uart2_callback1, 0);
#else
    int uart_id = DOORBELL_UART2_ID;
    uint32_t badurate = 9600;
    bkwl_uart_init(uart_id, badurate);

#endif
    tprintf("doorbell_uart2_init  1\n");
    ret = rtos_create_thread(doorbell_uart2_rec_thread_handle,
                             DOORBELL_TASK_PRIO,
                             "uart2_rcv",
                             (beken_thread_function_t)doorbell_uart2_receiver_thread,
                             DOORBELL_TASK_STACK_SIZE,
                             (beken_thread_arg_t)0);
    tprintf("doorbell_uart2_init  2\n");
    if (ret != kNoErr)
    {
        tprintf("doorbell_uart2_init  3\n");
        if (doorbell_uart2_rec_thread_handle != NULL)
        {
            tprintf("doorbell_uart2_init  4\n");
            rtos_delete_thread(&doorbell_uart2_rec_thread_handle);
        }
    }
    tprintf("doorbell_uart2_init  5\n");
    ret = rtos_create_thread(doorbell_uart2_send_thread_handle,
                             DOORBELL_TASK_PRIO,
                             "uart2_send",
                             (beken_thread_function_t)doorbell_uart2_send_thread,
                             DOORBELL_TASK_STACK_SIZE,
                             (beken_thread_arg_t)0);
    tprintf("doorbell_uart2_init  6\n");
    if (ret != kNoErr)
    {
        tprintf("doorbell_uart2_init  7\n");
        if (doorbell_uart2_send_thread_handle != NULL)
        {
            tprintf("doorbell_uart2_init  8\n");
            rtos_delete_thread(&doorbell_uart2_send_thread_handle);
        }
    }

    tprintf("doorbell_uart2_init  9\n");
    return 0;
}

void doorbell_uart2_receiver_thread(void)
{
    tprintf("doorbell_uart2_receiver_thread  entry\n");
    tprintf("%s \r\n", __func__);
    uart_write_string(UART_ID_2, "Helloworld\r\n");

    uint8_t *uart_data = (uint8_t *)os_malloc(DOORBELL_UART2_BUF_LEN);
    os_memset(uart_data, 0, DOORBELL_UART2_BUF_LEN);
    bk_set_printf_sync(true);
    while (1)
    {
        tprintf("doorbell_uart2_receiver_thread wait data\n\n");
        int len = bk_uart_read_bytes(DOORBELL_UART2_ID, uart_data, DOORBELL_UART2_BUF_LEN, DOORBELL_UART2_RX_TIMEOUT);

        if (len < 0)
        {
            tprintf("uart read failed, ret:-0x%x\r\n", -len);
        }
        else
        {
            os_printf("uart 2 =[%d][%s]\r\n", len, uart_data);

            // for (int i = 0; i < len; i++) {
            //     tprintf("%x ", uart_data[i]);
            // }
            // tprintf("\r\n");
        }
        os_memset(uart_data, 0, DOORBELL_UART2_BUF_LEN);
    }
}

void doorbell_uart2_send_thread(void)
{
    tprintf("%s \r\n", __func__);
    tprintf("doorbell_uart2_send_thread sennd Helloworld\n");
    // uart_write_string(UART_ID_2, "Helloworld\r\n");

    uint8_t *uart_data = (uint8_t *)os_malloc(DOORBELL_UART2_BUF_LEN);
    os_memset(uart_data, 0, DOORBELL_UART2_BUF_LEN);
    bk_set_printf_sync(true);

    char data_buf[20] = {0};
    while (1)
    {

        tprintf("doorbell_uart2_send_thread sennd Helloworld\n");

#if 0
           uart_write_string(UART_ID_2, "Helloworld\r\n");
#else
        strcpy(data_buf, "Helloworld\r\n");
        bkwl_uart_write(UART_ID_2, (uint8_t *)data_buf, strlen(data_buf));
#endif
        rtos_delay_milliseconds(1500);
        tprintf("doorbell_uart2_send_thread sennd belon\n");
#if 0
        uart_write_string(UART_ID_2, "belon\r\n");
#else

        strcpy(data_buf, "belon\r\n");
        bkwl_uart_write(UART_ID_2, (uint8_t *)data_buf, strlen(data_buf));
#endif

        rtos_delay_milliseconds(1500);
    }
}
